#include<bits/stdc++.h>
using namespace std;
int T,n,m,x[8003],y[8003],o[8003],f[60030],fcnt;
inline int getdis(int u,int v) {
    return max(max(x[u]-x[v],x[v]-x[u]),max(y[u]-y[v],y[v]-y[u]));
}
int hd[800],nxt[8003],tmpx,tmplst[8003];
long long ans,pre[8003][8003];
int xcnt,xlst[8003],ycnt,ylst[8003];
inline long long getsum(int xl,int xr,int yl,int yr,long long k) {//sum+k*cnt
    if (xl>xlst[xcnt]) return 0;
    if (yl>ylst[ycnt]) return 0;
    xl=lower_bound(xlst+1,xlst+1+xcnt,xl)-xlst-1;
    yl=lower_bound(ylst+1,ylst+1+ycnt,yl)-ylst-1;
    xr=upper_bound(xlst+1,xlst+1+xcnt,xr)-xlst-1;
    yr=upper_bound(ylst+1,ylst+1+ycnt,yr)-ylst-1;
    if ((xl>=xr)||(yl>=yr)) return 0;
    long long tmp=pre[xr][yr]-pre[xl][yr]-pre[xr][yl]+pre[xl][yl];
    return (tmp&((1ll<<33)-1))+k*(tmp>>33);
}
int main() {
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    for (int i=2;i<=60003;i++) f[i]=1;
    for (int i=2;i<=60003;i++) {
        if (!f[i]) continue;
        for (int j=i*2;j<=60003;j+=i) f[j]=0;
        if ((i>10)&&(i%20!=3)) f[i]=0;
        if (f[i]) f[i]=++fcnt;
    }
    scanf("%d",&T);
    while (T--) {
        xcnt=ycnt=0;
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++) {
            scanf("%d%d",&x[i],&y[i]);
            x[i]+=y[i];
            y[i]=x[i]-y[i]-y[i];
            y[i]+=m;
            xlst[i]=x[i];ylst[i]=y[i];
        }
        for (int i=1;i<=n;i++) scanf("%d",&o[i]);
        sort(xlst+1,xlst+1+n);
        xcnt=unique(xlst+1,xlst+1+n)-(xlst+1);
        sort(ylst+1,ylst+1+n);
        ycnt=unique(ylst+1,ylst+1+n)-(ylst+1);
        for (int i=1;i<=xcnt;i++) {
            for (int j=1;j<=ycnt;j++) {
                pre[i][j]=0;
            }
        }
        for (int i=1;i<=n;i++) {
            pre[lower_bound(xlst+1,xlst+1+xcnt,x[i])-xlst][lower_bound(ylst+1,ylst+1+ycnt,y[i])-ylst]+=o[i]+(1ll<<33);
        }
        for (int i=1;i<=xcnt;i++) {
            for (int j=1;j<=ycnt;j++) {
                pre[i][j]+=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
            }
        }
        ans=0;
        for (int i=1;i<=n;i++) {
            for (int j=1;j<=fcnt;j++) hd[j]=0;
            for (int j=1;j<=n;j++) {
                tmpx=f[getdis(i,j)];
                if (tmpx) {
                    nxt[j]=hd[tmpx];
                    hd[tmpx]=j;
                }
            }
            for (int t=1;t<=fcnt;t++) {
                tmpx=0;
                for (int i=hd[t];i;i=nxt[i]) tmplst[++tmpx]=i;
                for (int u=1;u<tmpx;u++) {
                    for (int v=u+1;v<=tmpx;v++) {
                        if ((f[getdis(tmplst[u],tmplst[v])]!=t)||(i<tmplst[u])&&(i<tmplst[v])) ans+=o[i]+o[tmplst[u]]+o[tmplst[v]];
                    }
                }
            }
        }
        for (int i=1;i<=n;i++) {
            for (int j=i+1;j<=n;j++) {
                if (!f[getdis(i,j)]) continue;
                int k=getdis(i,j);
                ans+=getsum(x[i]+1-k,x[i]-1+k,y[i]+1-k,y[i]-1+k,o[i]+o[j]);
                ans-=getsum(max(x[i]+1,x[j])-k,min(x[i]-1,x[j])+k,max(y[i]+1,y[j])-k,min(y[i]-1,y[j])+k,o[i]+o[j]);
                ans+=getsum(x[j]+1-k,x[j]-1+k,y[j]+1-k,y[j]-1+k,o[i]+o[j]);
                ans-=getsum(max(x[j]+1,x[i])-k,min(x[j]-1,x[i])+k,max(y[j]+1,y[i])-k,min(y[j]-1,y[i])+k,o[i]+o[j]);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
/*
dis[u][v]=max(x[u]-x[v],x[v]-x[u],y[u]-y[v],y[v]-y[u])
dis[i][u]<k:max(x[i]-x[u],x[u]-x[i],y[i]-y[u],y[u]-y[i])<k
x[i]<=x[u]-1+k x[i]>=x[u]+1-k y[i]<=y[u]-1+k y[i]>=y[u]+1-k
x[u]+1-k<=x[i]<=x[u]-1+k,y[u]+1-k<=y[i]<=y[u]-1+k

dis[i][u]<k,dis[i][v]<=k:
x[i]<=x[u]-1+k x[i]>=x[u]+1-k x[i]<=x[v]+k x[i]>=x[v]-k
y[i]<=y[u]-1+k y[i]>=y[u]+1-k y[i]<=y[v]+k y[i]>=y[v]-k
max(x[u]+1,x[v])-k<=x[i]<=min(x[u]-1,x[v])+k
max(y[u]+1,y[v])-k<=y[i]<=min(y[u]-1,y[v])+k
*/
